angular.module('starter').
factory("localStorage", ["$ionicPlatform", function(platform) {
  return {
    get: function(key, cbk) {
      platform.ready(function() {
        if (window.cordova && window.cordova.plugins.LocalStorage) {
          window.cordova.plugins.LocalStorage.get(key,
            function(value) {
              var v = JSON.parse(value);
              console.log("get: " + key + " ==> " + v);
              if (angular.isFunction(cbk)) {
                cbk(v);
              }
            })
        } else {
          console.error("unsupport platform");
        }
      })
    },
    set: function(key, value) {
      platform.ready(function() {
        if (window.cordova && window.cordova.plugins.LocalStorage) {
          var value_js = angular.toJson(value);
          console.log("set: " + key + " ==> " + value);
          window.cordova.plugins.LocalStorage.set(key,
            value_js);
        } else {
          console.error("unsupport platform");
        }
      })
    }
  }
}])
